﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace USB_HID
{
    public partial class Form1 : Form
    {
        Communication usbComm = new Communication();
       
        public Form1()
        {
            
            Console.WriteLine("statrt");
            usbComm.COMM_DeviceRemoved += COMM_DeviceRemoved;
            usbComm.COMM_ReceiveRecordNumber += COMM_RxRecordNumber;
            usbComm.COMM_ReceiveRecordTime += COMM_RxRecordTime;
            usbComm.COMM_ReceiveRecordData += COMM_RxRecordData;
            InitializeComponent();

            
        }

        void COMM_RxRecordData(string strData)
        {
            if (InvokeRequired)
            {
                Invoke(new RecordDataEventHanler(COMM_RxRecordData), new object[] {strData});
            }
            else
            {
                listBoxRecordData.Items.Add(strData);
            }
        }

        void COMM_RxRecordNumber(UInt32 number)
        {
            if (InvokeRequired)
            {
                Invoke(new RecordNumberEventHanler(COMM_RxRecordNumber), new object[] {number});
            }
            else
            {
                toolStripStatusLabel1.Text += ("  设备内存储记录" + number.ToString());
            }
        }
        void COMM_RxRecordTime(UInt32 number, byte[] time)
        {
            if (InvokeRequired)
            {
                Invoke(new RecordTimeEventHanler(COMM_RxRecordTime), new object[] {number, time});
            }
            else
            {
                int i;
                string str;
                listBoxRecordTime.Items.Clear();
                for (i = 0; i < number; i++)
                {
                    str = time[i * 2].ToString("00") + "时" + time[i * 2 + 1].ToString("00") + "分";
                    listBoxRecordTime.Items.Add(str);
                }
            }
        }


        void COMM_DeviceRemoved(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler(COMM_DeviceRemoved), new object[] { sender, e });
            }
            else
            {
                timerConnect.Start();
                toolStripStatusLabel1.Text = "设备未连接";
            }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            if (usbComm.usbSearchDeveice())
            {
                usbComm.SyncTime();
                usbComm.GetRecordNumber();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i = 0;

            for (i = 1; i <= 24; i++)
            {
                cmb_Hour.Items.Add(i.ToString("00") + "时");
            }

            for (i = 0; i < 60; i++)
            {
                cmb_Minute.Items.Add(i.ToString("00") + "分");
            }
            cmb_Hour.Text = "12时";
            cmb_Minute.Text = "00分";
            timerConnect.Interval = 1000;
            timerConnect.Start();
            //listBox2.ItemHeight = 30;
            


        }

        //private void button3_Click(object sender, EventArgs e)
        //{
        //    if (usbComm.recordNumber > 0)
        //    {
        //        string path = string.Empty;  //文件路径
        //        SaveFileDialog file = new SaveFileDialog();
        //        file.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        //
        //        if (file.ShowDialog() == DialogResult.OK)
        //        {
        //            usbComm.GetRecord();
        //        }
        //    }
        //}

        private void button2_Click_1(object sender, EventArgs e)
        {
            byte hour;
            byte minute;
            string str;

            int temp = int.Parse(cmb_Hour.SelectedItem.ToString().Substring(0, 2));
            hour = (byte)(temp & 0xff);

            temp = int.Parse(cmb_Minute.SelectedItem.ToString().Substring(0, 2));
            minute = (byte)(temp & 0xff);

            str = cmb_Hour.SelectedItem.ToString() + cmb_Minute.SelectedItem.ToString();

            if (listBoxRecordTime.Items.Contains(str))
            {
                MessageBox.Show("已添加该时间");
            }
            else
            {
                listBoxRecordTime.Items.Add(str);
            }
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (listBoxRecordTime.Items.Count > 0)
            {
                listBoxRecordTime.Items.Remove(listBoxRecordTime.SelectedItem);
            }
        }

        private void bt_writeDevice_Click(object sender, EventArgs e)
        {
            UInt32 length = (UInt32)listBoxRecordTime.Items.Count;
            byte[] data = new byte[length * 2];
            int i;
            string str;

            for (i = 0; i < listBoxRecordTime.Items.Count; i++)
            {
                str = listBoxRecordTime.Items[i].ToString();
                data[i * 2] = (byte)int.Parse(listBoxRecordTime.Items[i].ToString().Substring(0, 2));
                data[i * 2 + 1] = (byte)int.Parse(listBoxRecordTime.Items[i].ToString().Substring(3, 2));
            }
            usbComm.SyncRecordTime(length, data);
        }

        private void bt_readDevice_Click(object sender, EventArgs e)
        {
            usbComm.GetRecordTime();
        }

        private void timerConter_Tick(object sender, EventArgs e)
        {
            if (usbComm.usbSearchDeveice())
            {
                toolStripStatusLabel1.Text = "设备已连接";
                usbComm.SyncTime();
                usbComm.GetRecordTime();
                usbComm.GetRecordNumber();
                usbComm.GetRecord();
                timerConnect.Stop();
                //
                
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (listBoxRecordData.Items.Count > 0)
            {
                int i;
                string str;
                //string path = string.Empty;  //文件路径
                SaveFileDialog file = new SaveFileDialog();
                file.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                if (file.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter writeText = new StreamWriter(file.FileName, false, Encoding.GetEncoding("GB2312"));

                    for (i = 0; i < listBoxRecordData.Items.Count; i++)
                    {
                        str = listBoxRecordData.Items[i].ToString();
                        writeText.WriteLine(str);

                    }
                    writeText.Close();

                }
            }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            usbComm.ClearRecord();
        }
    } 
}
